package gameObject
{
	import away3d.containers.ObjectContainer3D;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.net.SharedObject;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.System;
	
	import gameObject.resources3D.MapResource;
	
	public class BattleMap extends MapBase
	{
		private var _id:int;
		private var _level:int;
		private var _cameraSetting:String = Global.iniAddress+"BW_Camera.xml";
		private var settingLoader:URLLoader;
		
		///////////////////
		
		private var _maxEnemy:uint;
		private var _enemyBornAreas:Vector.<EnemyBornArea>;
		
		/////////////////////////////////////////////////////
		
		/**
		 * @param ID 地图资源ID
		 * @param Level 地图刷怪等级
		 */		
		public function BattleMap(ID:int,Level:int)
		{
			_id = ID;
			_level = Level;
			
			_enemyBornAreas = new Vector.<EnemyBornArea>;
			
			_cameraRoot = new ObjectContainer3D;
			addChild(_cameraRoot);
			
			settingLoader = new URLLoader;//取相机
			settingLoader.addEventListener(Event.COMPLETE,cameraLoaded);
			settingLoader.load(new URLRequest(_cameraSetting));
		}
		/////////////////////////////////////////////////////
		private function cameraLoaded(e:Event):void//收到相机信息并处理
		{
			settingLoader.removeEventListener(Event.COMPLETE,cameraLoaded);
			
			var xml:XML = new XML(settingLoader.data);
			Global.gameView.getCamera(xml);
			_cameraRoot.addChild(Global.gameView.camera);
			
			if(Global.mapResources[_id]==undefined)
			{
				_resource = new MapResource(_id);//取出地图3D资源
				_resource.addEventListener(Event.COMPLETE,gotResource);
				_resource.load();
			}else
			{
				_resource = Global.mapResources[_id];
				setup();
			}
		}
		
		private function gotResource(e:Event):void//地图3D资源加载完成
		{
			_resource.removeEventListener(Event.COMPLETE,gotResource);
			
			setup();
		}
		
		private function setup():void
		{
			var mapRes:MapResource = _resource as MapResource;
			addChild(mapRes);
			
			if(mapRes.collisionMap)
			{
				CollisionWorld.collisionMap = new Bitmap(mapRes.collisionMap);
			
				CollisionWorld.mapScaleX = mapRes.collisionMap.width/mapRes.width;
				CollisionWorld.mapScaleY = mapRes.collisionMap.height/mapRes.height;
			
				CollisionWorld.hitStartX = - mapRes.startX;
				CollisionWorld.hitStartY = - mapRes.startY;
			}
			
			var levelDataURL:String = Global.iniAddress+"mapEnemySettings/map"+String(_id)+"_"+String(_level)+".xml";
			settingLoader.addEventListener(Event.COMPLETE,getBattleData);
			settingLoader.load(new URLRequest(levelDataURL));
		}
		
		private function getBattleData(e:Event):void
		{
			settingLoader.removeEventListener(Event.COMPLETE,getBattleData);
			var levelData:XML = new XML(settingLoader.data);
			
			_maxEnemy = uint(levelData.@maxEnemy);
			for each(var data:XML in levelData.area)
			{
				var borArea:EnemyBornArea = new EnemyBornArea(data.@posX,data.@posY,data.@wid,data.@hei,data.@offsetX,data.@offsetY);
				
				for each(var ene:XML in data.enemy)
					borArea.setNewEnemyType(ene.@type,ene.@count);
				
				_enemyBornAreas.push(borArea);
			}
			
			System.disposeXML(levelData);
			
			dispatchEvent(new Event(Event.COMPLETE));//资源收完后发出一个通知
		}
		
	}
}